package com.zzxz.practicaltrainingsys.mapper;

import com.zzxz.practicaltrainingsys.entity.Homeworkfinish;
import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Map;

@Mapper
public interface HomeworkfinishMapper {

    //查找所有完成的作业
    @Select("SELECT * FROM homeworkfinish")
    List<Homeworkfinish> queryAllHomeworkfinish();

    //查找某个项目的所有完成作业
    @Select("select * from homeworkfinish where project_id= #{id}")
    List<Homeworkfinish> getProHomeworkfinishById(Integer id);

    //根据homework_id查找这次作业的完成作业
    @Select("select * from homeworkfinish where homework_id= #{id}")
    List<Homeworkfinish> getHomeHomeworkfinishById(Integer id);

    //查看完成作业详情
    @Select("select * from homeworkfinish where finish_id= #{id}")
    Homeworkfinish getDetailHomeworkfinishById(Integer id);

    //管理员端查询所有完成作业（包含项目名称、作业名称、学生姓名）
    @Select("SELECT hf.*, p.project_name, s.student_name, hp.homework_name, hp.homework_introduction, hp.homework_type, hp.homework_category " +
            "FROM homeworkfinish hf " +
            "JOIN homeworkpublish hp ON hf.homework_id = hp.homework_id " +
            "JOIN project p ON hp.project_id = p.project_id " +
            "JOIN student s ON hf.student_id = s.student_id " +
            "ORDER BY hf.finish_id DESC")
    List<Map<String, Object>> getAllHomeworkfinish();

    // 根据学生ID获取已提交作业列表（修复：添加具体SQL实现）
    @Select("SELECT hf.*, hp.homework_name, hp.homework_introduction, " +
            "hp.homework_type, hp.homework_category, p.project_name " +
            "FROM homeworkfinish hf " +
            "JOIN homeworkpublish hp ON hf.homework_id = hp.homework_id " +
            "JOIN project p ON hp.project_id = p.project_id " +
            "WHERE hf.student_id = #{studentId} " +
            "ORDER BY hf.finish_time DESC")
    List<Homeworkfinish> getHomeworksByStudentId(@Param("studentId") Integer studentId);

    // 获取学生某个作业的提交详情（修复：添加具体SQL实现）
    @Select("SELECT * FROM homeworkfinish " +
            "WHERE student_id = #{studentId} AND homework_id = #{homeworkId}")
    Homeworkfinish getStudentHomeworkDetail(@Param("studentId") Integer studentId,
                                            @Param("homeworkId") Integer homeworkId);

    // 学生提交作业（修复：添加具体SQL实现）
    @Insert("INSERT INTO homeworkfinish " +
            "(student_id, homework_id, finish_content, finish_time) " +
            "VALUES " +
            "(#{student_id}, #{homework_id}, #{finish_content}, #{finish_time})")
    @Options(useGeneratedKeys = true, keyProperty = "finish_id")
    int insertHomeworkfinish(Homeworkfinish homeworkfinish);

    // 学生更新作业（修复：添加具体SQL实现）
    @Update("UPDATE homeworkfinish SET " +
            "finish_content = #{finish_content}, " +
            "finish_time = #{finish_time} ," +
            "homework_back = #{ homework_back} " +
            "WHERE finish_id = #{finish_id}")
    int updateHomeworkfinish(Homeworkfinish homeworkfinish);

    // 新增方法
    @Select("SELECT hf.* FROM homeworkfinish hf " +
            "JOIN team t ON hf.student_id = t.leader_id " +
            "WHERE t.team_id = #{teamId} AND hf.homework_id = #{homeworkId}")
    List<Homeworkfinish> getTeamHomeworkDetails(@Param("teamId") Integer teamId,
                                                @Param("homeworkId") Integer homeworkId);

    @Select("SELECT COUNT(*) FROM team WHERE team_id = #{teamId} AND leader_id = #{studentId}")
    int isTeamLeader(@Param("studentId") Integer studentId,
                     @Param("teamId") Integer teamId);

    @Select("SELECT t.* FROM team t " +
            "JOIN member m ON t.team_id = m.team_id " +
            "WHERE m.student_id = #{studentId} AND t.project_id = #{projectId}")
    Map<String, Object> getStudentTeamInfo(@Param("studentId") Integer studentId,
                                           @Param("projectId") Integer projectId);

    //批改个人作业
    @Update("UPDATE homeworkfinish SET " +
            "homework_marks = #{homeworkMarks}, " +
            "teacher_comment = #{teacherComment} " +
            "WHERE homework_id = #{homeworkId} AND student_id = #{studentId}")
    int gradeIndividualHomework(@Param("homeworkId") Integer homeworkId,
                                @Param("studentId") Integer studentId,
                                @Param("homeworkMarks") String homeworkMarks,
                                @Param("teacherComment") String teacherComment);

    //批改小组作业 - 批改小组长的作业记录
    @Update("UPDATE homeworkfinish hf " +
            "JOIN team t ON hf.student_id = t.leader_id " +
            "SET hf.homework_marks = #{homeworkMarks}, " +
            "    hf.teacher_comment = #{teacherComment} " +
            "WHERE hf.homework_id = #{homeworkId} AND t.team_id = #{teamId}")
    int gradeTeamHomework(@Param("homeworkId") Integer homeworkId,
                          @Param("teamId") Integer teamId,
                          @Param("homeworkMarks") String homeworkMarks,
                          @Param("teacherComment") String teacherComment);

    //打回个人作业
    @Update("UPDATE homeworkfinish SET " +
            "homework_back = #{homeworkBack}, " +
            "homework_marks = NULL, " +
            "teacher_comment = NULL " +
            "WHERE homework_id = #{homeworkId} AND student_id = #{studentId}")
    int returnIndividualHomework(@Param("homeworkId") Integer homeworkId,
                                 @Param("studentId") Integer studentId,
                                 @Param("homeworkBack") Integer homeworkBack);

    //打回小组作业 - 打回小组长的作业记录
    @Update("UPDATE homeworkfinish hf " +
            "JOIN team t ON hf.student_id = t.leader_id " +
            "SET hf.homework_back = #{homeworkBack}, " +
            "    hf.homework_marks = NULL, " +
            "    hf.teacher_comment = NULL " +
            "WHERE hf.homework_id = #{homeworkId} AND t.team_id = #{teamId}")
    int returnTeamHomework(@Param("homeworkId") Integer homeworkId,
                           @Param("teamId") Integer teamId,
                           @Param("homeworkBack") Integer homeworkBack);

}